<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师编号" prop="teacherid">
      <el-input v-model="dataForm.teacherid" placeholder="教师编号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="teachername">
      <el-input v-model="dataForm.teachername" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="institute">
      <el-input v-model="dataForm.institute" placeholder="学院"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pwd">
      <el-input v-model="dataForm.pwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="cardid">
      <el-input v-model="dataForm.cardid" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="职称" prop="type">
      <el-input v-model="dataForm.type" placeholder="职称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          teacherid: '',
          teachername: '',
          institute: '',
          sex: '',
          tel: '',
          email: '',
          pwd: '',
          cardid: '',
          type: ''
        },
        dataRule: {
          teacherid: [
            { required: true, message: '教师编号不能为空', trigger: 'blur' }
          ],
          teachername: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          institute: [
            { required: true, message: '学院不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          cardid: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '职称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/exam/teacher/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teacherid = data.teacher.teacherid
                this.dataForm.teachername = data.teacher.teachername
                this.dataForm.institute = data.teacher.institute
                this.dataForm.sex = data.teacher.sex
                this.dataForm.tel = data.teacher.tel
                this.dataForm.email = data.teacher.email
                this.dataForm.pwd = data.teacher.pwd
                this.dataForm.cardid = data.teacher.cardid
                this.dataForm.type = data.teacher.type
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/exam/teacher/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'teacherid': this.dataForm.teacherid,
                'teachername': this.dataForm.teachername,
                'institute': this.dataForm.institute,
                'sex': this.dataForm.sex,
                'tel': this.dataForm.tel,
                'email': this.dataForm.email,
                'pwd': this.dataForm.pwd,
                'cardid': this.dataForm.cardid,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
